package mop.domain.resource;

import java.sql.Timestamp;

import mop.exception.IllegalReservationException;

/**
 * The class to keep track of a reservation
 * 
 * @invar	the start time should come before the end time
 *  		| isValidTimeCouple(start,end)
 *  
 * @author	MOP Team 7
 */
public class Reservation 
{

	/**
	 * Construct a reservation object
	 * 
	 * @param 	start
	 * 	 		the start time in timestamp format
	 * 
	 * @param 	end
	 *   		the end time in time stamp format
	 *   
	 * @throws 	IllegalReservationException
	 *  		if the start !< end
	 *  		|!isValidTimeCouple(start, end)
	 */
	public Reservation(Timestamp start, Timestamp end) 
		throws IllegalReservationException
	{
		if(!isValidTimeCouple(start, end))
		{
			throw new IllegalReservationException(start,end);
		}
		
		this.start = start;
		this.end = end;
	}
	
	/***************************************************************************
	 * 							Time
	 ***************************************************************************/
	
	/**
	 * Returns the start Time.
	 * 
	 * @return 	this.start
	 */
	public Timestamp getStart()
	{
		return this.start;
	}
	
	/**
	 * Returns the end time.
	 * 
	 * @return 	this.end
	 */
	public Timestamp getEnd()
	{
		return this.end;
	}
	
	/**
	 * Checks whether a combination of 2 timestamps can form a valid reservation.
	 * 
	 * @param 	start
	 * 			the start time
	 * 
	 * @param 	end
	 *  		the end time
	 *  
	 * @return 	if start < end return true
	 */
	public static boolean isValidTimeCouple(Timestamp start, Timestamp end)
	{
		return start.before(end);
	}
	
	/**
	 * Checks whether the reservation is still valid.
	 * 
	 * @return	If the current time is larger than the end date,
	 * 			then this reservation is over.
	 */
	public boolean isDone()
	{
		Timestamp td = new Timestamp(System.currentTimeMillis());
		return td.after(this.end);
	}
	
	/**
	 * Checks whether the reservation is during the given time.
	 * 
	 * @param 	time
	 * 			A timestamp.
	 * 
	 * @pre		time is not null.
	 * 
	 * @return	True if the given time is "between" the time interval
	 * 			of the reservation. Otherwise false.
	 */
	public boolean isDuring(Timestamp time)
	{
		return start.before(time) && end.after(time);
	}
	
	/** 
	 * The start time.
	 */
	private Timestamp start;
	
	/**
	 * The end time.
	 */
	private Timestamp end;
	
	/**
	 * Returns a string representing this reservation.
	 * 
	 * @return A string representing this reservation.
	 */
	public String toString()
	{
		StringBuffer buffer = new StringBuffer();
		buffer.append("From ");
		buffer.append(start.getDate());
		buffer.append("-");
		buffer.append((start.getMonth()+1));
		buffer.append("-");
		buffer.append((start.getYear()+1900));
		buffer.append(" ");
		buffer.append(start.getHours());
		buffer.append(":");
		buffer.append(start.getMinutes());
		buffer.append(" till ");
		buffer.append(end.getDay());
		buffer.append("-");
		buffer.append((end.getMonth()+1));
		buffer.append("-");
		buffer.append((end.getYear()+1900));
		buffer.append(" ");
		buffer.append(end.getHours());
		buffer.append(":");
		buffer.append(end.getMinutes());
		return buffer.toString();
	}
	
}